<?php

/**
 * activite actions.
 *
 * @package		gesp
 * @subpackage activite
 * @author		 Your name here
 * @version		SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class activiteActions extends sfActions {
	public function executeIndex(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$this->group = $request->getParameter('group', 'hebdo');
		$q = Doctrine_Query::create()
			->from('Activite')
			->where('act_prj_id = ?', $prj_id)
			->andWhere('act_pere_id is null');
		$this->activites = $q->execute();
	}

	public function executeShow(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$this->forward404Unless($this->activite = Doctrine_Core::getTable('Activite')->find(array($request->getParameter('act_id'))), sprintf('Object activite does not exist (%s).', $request->getParameter('act_id')));
		$this->forward404Unless($this->activite->getActPrjId() == $prj_id);
	}

	public function executeStart(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$this->forward404Unless($activite = Doctrine_Core::getTable('Activite')->find(array($request->getParameter('act_id'))), sprintf('Object activite does not exist (%s).', $request->getParameter('act_id')));
		$this->forward404Unless($activite->getActPrjId() == $prj_id);
		$activite->start();
		$this->redirect('activite/index');
	}

	public function executeStop(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$last = ActiviteTable::getActiveActivite();
		$last->stop();
		$this->redirect('activite/index');
	}

	public function executeNew(sfWebRequest $request) {
		$prj_id = $this->getUser()->getAttribute('prj_id');
		$activite = new Activite();
		$activite->setActPrjId($prj_id);
		$this->form = new ActiviteForm($activite);
	}

	public function executeCreate(sfWebRequest $request) {
		$this->forward404Unless($request->isMethod(sfRequest::POST));

		$this->form = new ActiviteForm();

		$this->processForm($request, $this->form);

		$this->setTemplate('new');
	}

	public function executeEdit(sfWebRequest $request)
	{
		$this->forward404Unless($activite = Doctrine_Core::getTable('Activite')->find(array($request->getParameter('act_id'))), sprintf('Object activite does not exist (%s).', $request->getParameter('act_id')));
		$this->form = new ActiviteForm($activite);
	}

	public function executeUpdate(sfWebRequest $request)
	{
		$this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
		$this->forward404Unless($activite = Doctrine_Core::getTable('Activite')->find(array($request->getParameter('act_id'))), sprintf('Object activite does not exist (%s).', $request->getParameter('act_id')));
		$this->form = new ActiviteForm($activite);

		$this->processForm($request, $this->form);

		$this->setTemplate('edit');
	}

	public function executeDelete(sfWebRequest $request)
	{
		$request->checkCSRFProtection();

		$this->forward404Unless($activite = Doctrine_Core::getTable('Activite')->find(array($request->getParameter('act_id'))), sprintf('Object activite does not exist (%s).', $request->getParameter('act_id')));
		$activite->delete();

		$this->redirect('activite/index');
	}

	protected function processForm(sfWebRequest $request, sfForm $form) {
		$form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
		if ($form->isValid()) {
			$activite = $form->save();
			$this->redirect('activite/index');
		}
	}
}
